package com.zhfi.dataapply.schedule.reader.market;

import com.zhfi.dataapply.entity.data.po.market.ShareStructurePO;
import com.zhfi.dataapply.entity.data.vo.market.ShareStructureVO;
import com.zhfi.dataapply.schedule.companylist.CompanyListService;
import com.zhfi.dataapply.schedule.reader.AbstractKeyReader;
import com.zhfi.dataapply.service.CVSourceUrlService;
import com.zhfi.dataapply.service.ForwardService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Component
public class ShareStructureReader extends AbstractKeyReader<ShareStructureVO> {

    private static String URL_ID = "10.7";

    @Getter
    private String url;

    @Autowired
    private CVSourceUrlService cvSourceUrlService;

    @Autowired
    public ShareStructureReader(ForwardService forwardService,
                              CompanyListService companyListService) {
        super(ShareStructureVO.class, forwardService, companyListService);
    }

    protected void resetUrl() {
        this.url = cvSourceUrlService.getUrlById(URL_ID);
    }

    @Override
    public ShareStructureVO read() {
        ShareStructureVO vo = super.read();
        if (null == vo) return null;
        List<ShareStructurePO> shareStructureList = vo.getShareStructureList();
        if (!CollectionUtils.isEmpty(shareStructureList)) {
            shareStructureList.forEach(po -> {
                po.setCompanyId(vo.getCompanyId());
                po.setPubDate(vo.getPubDate());
            });
        }
        return vo;
    }

}
